//
//  BKStatusPictureView.swift
//  微博
//
//  Created by mac on 16/8/14.
//  Copyright © 2016年 xbk. All rights reserved.
//

import UIKit
import SDWebImage

private let StatusPictureViewCellIdentifier = "StatusPictureViewCellIdentifier"

//每项之间的间距
private let itemMargin:CGFloat = 5
//每项的宽度
private let itemWidth = (ScreenWidth - 2 * StatusTableViewCellMargin - 2 * itemMargin)/3

class BKStatusPictureView: UICollectionView {
    
    
    //显示配图的数据源
    var picUrls:[BKStatusPictureInfo]?{
    
        didSet {
            messageLabel.text = "\(picUrls?.count ?? 0)"
            let size = calcSizeWithCount(picUrls?.count ?? 0)
            self.snp_updateConstraints { (make) -> Void in
                
                make.size.equalTo(size)
            }
            self.reloadData()
        }
    }
    //显示配图的个数
    private lazy var messageLabel:UILabel = UILabel(fontSize: 20, textColor: UIColor.whiteColor())
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        
        let flowLayout = UICollectionViewFlowLayout()
        
        flowLayout.itemSize = CGSize(width: itemWidth, height: itemWidth)
        flowLayout.minimumInteritemSpacing = itemMargin
        flowLayout.minimumLineSpacing = itemMargin
        
        super.init(frame: frame, collectionViewLayout: flowLayout)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //添加控件设置约束
    private func setupUI() {
    
        dataSource = self
        //注册cell
        registerClass(BKStatusPictureViewCell.self, forCellWithReuseIdentifier: StatusPictureViewCellIdentifier)
        
        addSubview(messageLabel)
        messageLabel.snp_makeConstraints { (make) -> Void in
            
            make.center.equalTo(self)
        }
    }
    
    //通过配图的个数计算配图的大小
    private func calcSizeWithCount (count:Int)->CGSize{
    
        //列数
        let cols = count > 3 ? 3: count
        //行数
        let rows = (count - 1)/3 + 1
        
        //计算配图的大小
        //宽度
        let currentWidth = itemWidth * CGFloat(cols)  + CGFloat(cols - 1) * itemMargin
        //高度
        let currentHeight = itemWidth * CGFloat(rows) + CGFloat (cols - 1) * itemMargin
        
        return CGSize(width: currentWidth, height: currentHeight)
    }
    
}





extension BKStatusPictureView : UICollectionViewDataSource{

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return picUrls?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(StatusPictureViewCellIdentifier, forIndexPath: indexPath) as! BKStatusPictureViewCell
        cell.piInfo = picUrls![indexPath.item]
        return cell
    }
}

//自定义配图cell
class BKStatusPictureViewCell:UICollectionViewCell {
    
    var piInfo :BKStatusPictureInfo? {
    
        didSet {
        
            if let url = piInfo?.thumbnail_pic{
            
                imageView.sd_setImageWithURL(NSURL(string: url), placeholderImage: UIImage(named: "timeline_image_placeholder"))
                
                gifImagView.hidden = !url.hasPrefix(".gif")
            }
        }
    }
    
    //  MARK: -- 懒加载控件
    private lazy var imageView:UIImageView = {
    
        let view = UIImageView(image: UIImage(named: "timeline_image_placeholder"))
        view.contentMode = UIViewContentMode.ScaleAspectFill
        view.clipsToBounds = true
        return view
    }()
    
    private lazy var gifImagView:UIImageView = UIImageView(image:UIImage(named: "timeline_image_gif"))
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //添加控件设置约束
    private func setupUI(){
        contentView.addSubview(imageView)
        contentView.addSubview(gifImagView)
        
        //backgroundColor = RandomColor()
        
        imageView.snp_makeConstraints { (make) -> Void in
            
            make.edges.equalTo(contentView).offset(UIEdgeInsetsZero)
        }
        
        gifImagView.snp_makeConstraints { (make) -> Void in
            
            make.trailing.equalTo(imageView)
            make.bottom.equalTo(imageView)
        }
    }
    
}



